<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="物种名称" prop="speciesName">
        <el-input
          v-model="queryParams.speciesName"
          placeholder="请输入物种名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生物种类" prop="biologicalType">
        <el-input
          v-model="queryParams.biologicalType"
          placeholder="请输入生物种类"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="学名" prop="scientificName">-->
<!--        <el-input-->
<!--          v-model="queryParams.scientificName"-->
<!--          placeholder="请输入学名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最低适宜水温" prop="minTemp">-->
<!--        <el-input-->
<!--          v-model="queryParams.minTemp"-->
<!--          placeholder="请输入最低适宜水温"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最高适宜水温" prop="maxTemp">-->
<!--        <el-input-->
<!--          v-model="queryParams.maxTemp"-->
<!--          placeholder="请输入最高适宜水温"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最低溶氧" prop="minOxygen">-->
<!--        <el-input-->
<!--          v-model="queryParams.minOxygen"-->
<!--          placeholder="请输入最低溶氧"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最高溶氧" prop="maxOxygen">-->
<!--        <el-input-->
<!--          v-model="queryParams.maxOxygen"-->
<!--          placeholder="请输入最高溶氧"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最低氨氮" prop="minAmmonia">-->
<!--        <el-input-->
<!--          v-model="queryParams.minAmmonia"-->
<!--          placeholder="请输入最低氨氮"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最高氨氮" prop="maxAmmonia">-->
<!--        <el-input-->
<!--          v-model="queryParams.maxAmmonia"-->
<!--          placeholder="请输入最高氨氮"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="幼体投喂频率" prop="feedingFreqJuvenile">-->
<!--        <el-input-->
<!--          v-model="queryParams.feedingFreqJuvenile"-->
<!--          placeholder="请输入幼体投喂频率"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="成体投喂频率" prop="feedingFreqAdult">-->
<!--        <el-input-->
<!--          v-model="queryParams.feedingFreqAdult"-->
<!--          placeholder="请输入成体投喂频率"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!-- <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
<!--      <el-form-item label="创建时间" prop="createdAt">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.createdAt"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择创建时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="修改人" prop="updatedBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.updatedBy"-->
<!--          placeholder="请输入修改人"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="修改时间" prop="updatedAt">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.updatedAt"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择修改时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['SysAquaticSpecies:SysAquaticSpecies:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['SysAquaticSpecies:SysAquaticSpecies:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['SysAquaticSpecies:SysAquaticSpecies:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['SysAquaticSpecies:SysAquaticSpecies:export']"-->
<!--        >导出</el-button>-->
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SysAquaticSpeciesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="物种名称" align="center" prop="speciesName" />
      <el-table-column label="生物种类" align="center" prop="biologicalType" />
      <el-table-column label="学名" align="center" prop="scientificName" :show-overflow-tooltip="true" />
<!--      <el-table-column label="最低适宜水温" align="center" prop="minTemp" />-->
<!--      <el-table-column label="最高适宜水温" align="center" prop="maxTemp" />-->
<!--      <el-table-column label="最低溶氧" align="center" prop="minOxygen" />-->
<!--      <el-table-column label="最高溶氧" align="center" prop="maxOxygen" />-->
<!--      <el-table-column label="最低氨氮" align="center" prop="minAmmonia" />-->
<!--      <el-table-column label="最高氨氮" align="center" prop="maxAmmonia" />-->
<!--      <el-table-column label="最高盐度" align="center" prop="maxSalinity" />-->
<!--      <el-table-column label="最低盐度" align="center" prop="minSalinity" />-->
<!--      <el-table-column label="最高亚硝酸盐" align="center" prop="maxNitrite" />-->
<!--      <el-table-column label="最低亚硝酸盐" align="center" prop="minNitrite" />-->
<!--      <el-table-column label="最高PH" align="center" prop="maxPh" />-->
<!--      <el-table-column label="最低PH" align="center" prop="minPh" />-->
      <el-table-column label="幼体投喂频率" align="center" prop="feedingFreqJuvenile" />
      <el-table-column label="成体投喂频率" align="center" prop="feedingFreqAdult" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="物种图" align="center" prop="imageUrl" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="简介" align="center" prop="description" :show-overflow-tooltip="true" />-->
<!--      <el-table-column label="创建人" align="center" prop="createdBy" />-->
<!--      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="修改人" align="center" prop="updatedBy" />-->
<!--      <el-table-column label="修改时间" align="center" prop="updatedAt" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['SysAquaticSpecies:SysAquaticSpecies:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['SysAquaticSpecies:SysAquaticSpecies:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改养殖生物信息对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="SysAquaticSpeciesRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="13">
          <el-col :span="8">
            <el-form-item label="物种名称" prop="speciesName">
              <el-input v-model="form.speciesName" placeholder="请输入物种名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生物种类" prop="biologicalType">
              <el-input v-model="form.biologicalType" placeholder="请输入生物种类" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学名" prop="scientificName">
              <el-input v-model="form.scientificName" placeholder="请输入学名" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="13">
          <el-col :span="8">
            <el-form-item label="最低溶氧" prop="minOxygen">
              <el-input v-model="form.minOxygen" placeholder="请输入最低溶氧" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最低氨氮" prop="minAmmonia">
              <el-input v-model="form.minAmmonia" placeholder="请输入最低氨氮" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最低盐度" prop="minSalinity">
              <el-input v-model="form.minSalinity" placeholder="请输入最低盐度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="13">
          <el-col :span="8">
            <el-form-item label="最高溶氧" prop="maxOxygen">
              <el-input v-model="form.maxOxygen" placeholder="请输入最高溶氧" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最高氨氮" prop="maxAmmonia">
              <el-input v-model="form.maxAmmonia" placeholder="请输入最高氨氮" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最高盐度" prop="maxSalinity">
              <el-input v-model="form.maxSalinity" placeholder="请输入最高盐度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="13">
          <el-col :span="8">
            <el-form-item label="最高PH" prop="maxPh">
              <el-input v-model="form.maxPh" placeholder="请输入最高PH" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最高适宜水温" prop="maxTemp">
              <el-input v-model="form.maxTemp" type="number" placeholder="请输入最高适宜水温" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最高亚硝酸盐" prop="maxNitrite">
              <el-input v-model="form.maxNitrite" placeholder="请输入最高亚硝酸盐" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="13">
          <el-col :span="8">
            <el-form-item label="最低PH" prop="minPh">
              <el-input v-model="form.minPh" placeholder="请输入最低PH" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最低适宜水温" prop="minTemp">
              <el-input v-model="form.minTemp" type="number" placeholder="请输入最低适宜水温" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最低亚硝酸盐" prop="minNitrite">
              <el-input v-model="form.minNitrite" placeholder="请输入最低亚硝酸盐" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="幼体投喂频率" prop="feedingFreqJuvenile">
          <el-input v-model="form.feedingFreqJuvenile" placeholder="请输入幼体投喂频率" />
        </el-form-item>
        <el-form-item label="成体投喂频率" prop="feedingFreqAdult">
          <el-input v-model="form.feedingFreqAdult" placeholder="请输入成体投喂频率" />
        </el-form-item>
        <el-form-item label="物种图" prop="imageUrl">
          <image-upload v-model="form.imageUrl"/>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysAquaticSpecies">
import { listSysAquaticSpecies, getSysAquaticSpecies, delSysAquaticSpecies, addSysAquaticSpecies, updateSysAquaticSpecies, logicDeleteSysAquaticSpecies } from "@/api/SysAquaticSpecies/SysAquaticSpecies"
import { listSysTanks } from '@/api/SysTanks/SysTanks'
const { proxy } = getCurrentInstance()

const SysAquaticSpeciesList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    speciesName: null,
    biologicalType: null,
    scientificName: null,
    minTemp: null,
    maxTemp: null,
    minOxygen: null,
    maxOxygen: null,
    minAmmonia: null,
    maxAmmonia: null,
    feedingFreqJuvenile: null,
    feedingFreqAdult: null,
    imageUrl: null,
    description: null,
    createdBy: null,
    createdAt: null,
    updatedBy: null,
    updatedAt: null,
    status: null,
    minSalinity:null,
    maxSalinity:null,
    minNitrite:null,
    maxNitrite:null,
    maxPh:null,
    minPh:null
  },
})
const rules = ref({
    speciesName: [
      { required: true, message: "物种名称不能为空", trigger: "blur" },
      { max: 50, message: "物种名称长度不能超过50个字符", trigger: "blur" }
    ],
    biologicalType: [
      { required: true, message: "生物种类不能为空",trigger: "blur" },
      { max: 20, message: "生物种类长度不能超过20个字符", trigger: "blur" }
    ],
    scientificName: [
      { required: false, message: "学名不能为空", trigger: "blur" },
      { max: 100, message: "学名长度不能超过100个字符",trigger: "blur" }
    ],
    minTemp: [
      { required: false, message: '最低适宜水温不能为空', trigger: 'blur' },
      { pattern: /^\d+(\.\d{1})?$/, message: '请按照规定的数据格式进行输入', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.9) {
            callback(new Error("数值过大，请输入不超过 99.9 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    maxTemp: [
      { required: false, message: '最高适宜水温不能为空', trigger: 'blur' },
      { pattern: /^\d+(\.\d{1})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.9) {
            callback(new Error("数值过大，请输入不超过 99.9 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    minOxygen: [
      { required: false, message: '最低溶氧不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 9.9) {
            callback(new Error("数值过大，请输入不超过 9.9 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    maxOxygen: [
      { required: false, message: '最高溶氧不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.9) {
            callback(new Error("数值过大，请输入不超过 99.9 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    minAmmonia: [
      { required: false, message: '最低氨氮不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 9.99) {
            callback(new Error("数值过大，请输入不超过 9.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    maxAmmonia: [
      { required: false, message: '最高氨氮不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 9.99) {
            callback(new Error("数值过大，请输入不超过 9.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    minSalinity: [
      { required: false, message: '最低盐度不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.99) {
            callback(new Error("数值过大，请输入不超过 99.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    maxSalinity: [
      { required: false, message: '最高盐度不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.99) {
            callback(new Error("数值过大，请输入不超过 99.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    minNitrite: [
      { required: false, message: '最低亚硝酸盐不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.99) {
            callback(new Error("数值过大，请输入不超过 99.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    maxNitrite: [
      { required: false, message: '最高亚硝酸盐不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.99) {
            callback(new Error("数值过大，请输入不超过 99.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    minPh: [
      { required: false, message: '最低PH不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.99) {
            callback(new Error("数值过大，请输入不超过 99.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    maxPh: [
      { required: false, message: '最高PH不能为空',trigger: 'blur' },
      { pattern: /^\d+(\.\d{1,2})?$/, message: '请按照规定的数据格式进行输入',trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value && Number(value) > 99.99) {
            callback(new Error("数值过大，请输入不超过 99.99 的数值"))
          } else {
            callback()
          }
        },
        trigger: "blur"
      }
    ],
    description: [
      { required: false, message: "简介不能为空",trigger: "blur" },
      { max: 500, message: "简介长度不能超过500个字符",trigger: "blur" }
    ]
})

const { queryParams, form} = toRefs(data)

/** 查询养殖生物信息列表 */
function getList() {
  loading.value = true
  const params = { ...queryParams.value, status: 1 }
  listSysAquaticSpecies(params).then(response => {
    SysAquaticSpeciesList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    speciesName: null,
    biologicalType: null,
    scientificName: null,
    minTemp: null,
    maxTemp: null,
    minOxygen: null,
    maxOxygen: null,
    minAmmonia: null,
    maxAmmonia: null,
    feedingFreqJuvenile: null,
    feedingFreqAdult: null,
    imageUrl: null,
    description: null,
    createdBy: null,
    createdAt: null,
    updatedBy: null,
    updatedAt: null,
    status: null
  }
  proxy.resetForm("SysAquaticSpeciesRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加养殖生物信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getSysAquaticSpecies(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改养殖生物信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["SysAquaticSpeciesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSysAquaticSpecies(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSysAquaticSpecies(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
// function handleDelete(row) {
//   const _ids = row.id || ids.value
//   proxy.$modal.confirm('是否确认删除养殖生物信息编号为"' + _ids + '"的数据项？').then(function() {
//     return delSysAquaticSpecies(_ids)
//   }).then(() => {
//     getList()
//     proxy.$modal.msgSuccess("删除成功")
//   }).catch(() => {})
// }

// function handleDelete(row) {
//   const _ids = row.id || ids.value;

//   // 先校验是否正在养殖中使用
//   listSysTanks({ speciesId: _ids, isFinished: '1', pageNum: 1, pageSize: 1 }).then(res => {
//     if (res.total > 0) {
//       proxy.$modal.msgWarning('该生物正在被养殖中的桶使用，无法删除');
//       return;
//     }

//     proxy.$modal.confirm('是否确认删除养殖生物信息编号为"' + _ids + '"的数据项？')
//       .then(() => logicDeleteSysAquaticSpecies(_ids))   // ← 只传 ids 数组
//       .then(() => {
//         proxy.$modal.msgSuccess('删除成功');
//         getList();
//       });
//   });
// }


/** 删除按钮（顶部+行内） */
function handleDelete(row) {
  // 1. 统一拿 ID 数组
  const idList = row?.id ? [row.id] : ids.value
  if (!idList.length) {
    proxy.$modal.msgWarning('请选择要删除的物种')
    return
  }

  // 2. 建立 id -> 物种编码 映射
  const codeMap = {}
  SysAquaticSpeciesList.value.forEach(r => { codeMap[r.id] = r.speciesName })

  // 3. 一次性查出“正在使用”的物种 id 集合
  Promise.all(
    idList.map(id =>
      listSysTanks({ speciesId: id, isFinished: '1', pageNum: 1, pageSize: 1 })
        .then(res => ({ id, total: res.total }))
    )
  ).then(checkList => {
    const used = checkList.filter(it => it.total > 0)
    if (used.length) {
      // 用物种编码提示
      const codes = used.map(it => codeMap[it.id]).filter(Boolean)
      proxy.$modal.msgWarning(
        `有养殖桶正在养殖${codes.join(',')}，无法删除`
      )
      return
    }

    // 4. 逻辑删除
    return proxy.$modal
      .confirm(`是否确认删除选中的 ${idList.length} 个物种？`)
      .then(() => logicDeleteSysAquaticSpecies(idList))
      .then(() => {
        proxy.$modal.msgSuccess('删除成功')
        getList()
      })
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('SysAquaticSpecies/SysAquaticSpecies/export', {
    ...queryParams.value
  }, `SysAquaticSpecies_${new Date().getTime()}.xlsx`)
}

getList()
</script>
